package com.loveyukari.dao.impl;

import com.loveyukari.dao.ManageAuthDao;
import com.loveyukari.model.ManageAuth;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcManageAuthDao extends BaseDao implements ManageAuthDao {

    private RowMapper<ManageAuth> manageAuthRowMapper = new RowMapper<ManageAuth>() {
        @Override
        public ManageAuth mapRow(ResultSet resultSet, int i) throws SQLException {
            ManageAuth manageAuth = new ManageAuth();
            manageAuth.setId(resultSet.getInt("id"));
            manageAuth.setAreaCode(resultSet.getInt("area_code"));
            manageAuth.setAuthority(resultSet.getInt("authority"));
            manageAuth.setNama(resultSet.getString("name"));
            manageAuth.setPhone(resultSet.getString("phone"));
            manageAuth.setAuthId(resultSet.getInt("auth_id"));
            return manageAuth;
        }
    };


    @Override
    public ManageAuth selectByPhone(String phone) {
        String sql = "select * from manage_auth where phone = ?";
        List<ManageAuth> list = this.getJdbcTemplate().query(sql, manageAuthRowMapper, phone);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<ManageAuth> selectAll() {
        String sql = "select * from manage_auth ";
        List<ManageAuth> list = this.getJdbcTemplate().query(sql, manageAuthRowMapper);
        return list;
    }
}
